package com.gitee.wsl.collections.internal

import kotlin.contracts.ExperimentalContracts
import kotlin.contracts.contract
import kotlin.jvm.JvmField

@JvmField
internal val EMPTY_INTS = IntArray(0)

@JvmField
internal val EMPTY_OBJECTS = arrayOfNulls<Any>(0)

@JvmField
internal val EMPTY_LONGS = LongArray(0)


// An empty array of floats
internal val EmptyFloatArray = FloatArray(0)

internal fun idealIntArraySize(need: Int): Int {
    return idealByteArraySize(need * 4) / 4
}

internal fun idealLongArraySize(need: Int): Int {
    return idealByteArraySize(need * 8) / 8
}

internal fun idealByteArraySize(need: Int): Int {
    for (i in 4..31) {
        if (need <= (1 shl i) - 12) {
            return (1 shl i) - 12
        }
    }
    return need
}

// null-safe object equals, which is equivalent to `a == b || (a != null && a.equals(b));` in Java.
internal fun equal(a: Any?, b: Any?): Boolean {
    return a == b
}

// Like Kotlin's require() but without the .toString() call
@Suppress("BanInlineOptIn") // same opt-in as using Kotlin's require()
@OptIn(ExperimentalContracts::class)
internal inline fun requirePrecondition(value: Boolean, lazyMessage: () -> String) {
    contract {
        returns() implies value
    }
    if (!value) {
        throwIllegalArgumentException(lazyMessage())
    }
}

@Throws(IllegalArgumentException::class)
internal  fun throwIllegalArgumentException(message: String) {
    throw IllegalArgumentException(message)
}